
#ifndef THREAD
#define THREAD


#include <map>
#include <vector>
#include <string>
#include <list>
#include "WorkCell.h"
#include "IncludeHeader.h"


#ifdef WIN32
#include <Windows.h>
#define THREAD_ID HANDLE
#define THREAD_EVENT HANDLE
#endif


#ifdef METROL
#define THREAD_ID void*
#define THREAD_EVENT void*
#endif






class MainThreadWorkCell : public Cell
{
public :
	MainThreadWorkCell();
	~MainThreadWorkCell();
};


//class ThreadEvent
//{
//public :
//	ThreadEvent();
//	~ThreadEvent();
//private :
//
//};

enum ThreadEventType
{
	ADD_CELL,
	REMOVE_WORKCELL
};

struct ThreadEvent
{
	ThreadEventType Event;
	void* p_EventEntity;
};

typedef std::vector<ThreadEvent*> THREAD_EVENT_VECTOR;
typedef std::list<ThreadEvent*> THREAD_EVENT_CHUNK;

enum ThreadWorkerState
{
	/////////////////
	THREAD_INITIALIZING,
	/////////////////
	THREAD_READY,
	THREAD_RUNNING_NOTHING,
	/////////////////
	THREAD_WAITING,
	THREAD_LONG_WAITING,
	THREAD_HUNGER,
	/////////////////
	THREAD_BUSY,
	THREAD_VERY_BUSY,
	THREAD_ABNORMALY_BUSY,
	/////////////////
	THREAD_SLEEP,
	THREAD_SLEEP_TOO_LONG,
	THREAD_SLEEPING_DEAD,
	/////////////////
	THREAD_BLOCK_WAITHING_IO,
	/////////////////
    THREAD_JOB_WORKING,
	THREAD_JOB_FINISHED,
	/////////////////
	THREAD_DIE
};

#define MAX_SIGNAL_COUNT 2000

class ThreadWorker
{
public :
	ThreadWorker();
	ThreadWorker(char* name);
	//ThreadWorker(int Identifier = -1,THREA_PORCEDURE Procedure = NULL);
	virtual ~ThreadWorker();
public :
	virtual bool Init();
	//bool Init();
	//void ThreadWorkerRun();
	virtual bool StartWork();
	virtual bool StopWork(){return true;}
	virtual bool KillMyself(){return true;}
	virtual bool OnInit(void* pParemeter = NULL);
	//virtual bool OnEvent();
	virtual bool OnWork();
	virtual bool OnWorkFinished();
	virtual void OnThreadEvent();
	virtual void OnThreadState();
	virtual void AddSignalEvent(ThreadEvent* pEvent);
	ThreadWorkerState GetThreadWorkerState(){return m_State;}
	void AddCell(Cell* pCell);
	
	void UpdateTime(double fTime, float fElapsedTime);

	virtual bool Release(){return true;}

public :
	std::string m_ThreadName;
	THREAD_ID  m_Identifier;
	void* m_ThreadWorkBenginEvent;
	void* m_ThreadWorkFinshedEvent;
	void* m_ThreadWorkSemaphore;
	void SetState(ThreadWorkerState State);
protected :
	THREAD_EVENT_CHUNK m_ThreadWorkerEventVector;
	THREAD_EVENT_CHUNK m_ThreadWorkerEventVectorPreProcess;
protected :
	ThreadWorkerState m_State;
	double m_CreateTime;
	double m_LivingTime;
	double m_WorkingLastTime;
	double m_DieTime;
	CELL_VECTOR m_ThreadCellVector;
	//THREA_PORCEDURE m_Procedure;	
};




typedef std::map<std::string,ThreadWorker*> THREAD_WORKER_MAP;
typedef std::list<ThreadWorker*> THREAD_WORKER_LIST;   
typedef std::vector<ThreadWorker*> THREAD_WORKER_VECTOR;

class ThreadControler
{
public :
	ThreadControler();
	~ThreadControler();
public :
	virtual bool Initialize();
	virtual int  GetSuitableThreadCounts();
	virtual bool CreateSuitableThreadModle();
	virtual bool InitThread(ThreadWorker* thread);
	virtual bool Work(){return true;}
	virtual void CheckSynFinish(){}
	bool RunThreadWorker(std::string worker_name);
	bool StopThreadWorker(std::string worker_name);
	bool KillThreadWorker(std::string worker_name);
	void CheckWorkersStateAndHandleEvent();
	virtual bool Release(){return true;}
protected :
	int GetFreeThreadControlObjectIndex();
	void MarkThreadControlObject(int index,void* control_object);
protected :
	THREAD_WORKER_MAP m_ThreadWorkerMap;
	THREAD_WORKER_VECTOR m_UnknowThreadsVector;
	int m_SuitableThreadCounts;
	void** m_ThreadControlObject;

};

#endif
